package com.kotlin.hxh.ui.view

import android.content.Context
import android.graphics.Color
import android.os.Parcel
import android.os.Parcelable
import androidx.core.view.ViewCompat
import android.util.AttributeSet
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.LinearLayout

/**
 * @author huxh
 * @date 2018/12/20.
 */
class BottomBar : LinearLayout {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr) {
        init(context, attributeSet)
    }

    companion object {
     private   val TRANSLATE_DURATION_MILLIS = 200L
    }

    private lateinit var mTabLayout: LinearLayout
    private lateinit var mTabParams: LayoutParams
    private var mListener: OnTabSelectedListener? = null
    private var mCurrentPosition = 0
    private var mVisible = true
    private val mInterpolator = AccelerateDecelerateInterpolator()
    private fun init(context: Context, attributeSet: AttributeSet?) {
        mTabLayout = LinearLayout(context)
        mTabLayout.setBackgroundColor(Color.WHITE)
        mTabLayout.orientation = HORIZONTAL
        addView(mTabLayout, LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        mTabParams = LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT)
        mTabParams.weight = 1F
    }

    fun addItem(tab: BottomBarTab): BottomBar {
        tab.setOnClickListener {
            if (mListener == null) return@setOnClickListener
            val pos = tab.getTabPosition()
            if (mCurrentPosition == pos) {
                mListener?.onTabReselected(pos)
            } else {
                mListener?.onTabSelected(pos, mCurrentPosition)
                tab.isSelected = true
                mListener?.onTabUnselected(mCurrentPosition)
                mTabLayout.getChildAt(mCurrentPosition).isSelected = false
                mCurrentPosition = pos
            }
        }
        tab.setTabPosition(mTabLayout.childCount)
        tab.layoutParams = mTabParams
        mTabLayout.addView(tab)
        return this
    }

    fun setOnTabSelectedListener(onTabSelectedListener: OnTabSelectedListener) {
        mListener = onTabSelectedListener
    }

    fun setCurrentItem(postion: Int) {
        mTabLayout.post { mTabLayout.getChildAt(postion).performClick() }
    }


    override fun onSaveInstanceState(): Parcelable? {
        val superState = super.onSaveInstanceState()
        return SavedState(superState, mCurrentPosition)
    }

    override fun onRestoreInstanceState(state: Parcelable?) {
        state as SavedState
        super.onRestoreInstanceState(state.superState)
        if (mCurrentPosition != state.postion) {
            mTabLayout.getChildAt(mCurrentPosition).isSelected = false
            mTabLayout.getChildAt(state.postion).isSelected = true
        }
        mCurrentPosition = state.postion
    }

    fun getCurrentItemPosition() = mCurrentPosition
    fun isVisible() = mVisible
    fun hide(anim: Boolean = true) {
        toggle(false, anim, false)
    }

    fun show(anim: Boolean = true) {
        toggle(true, anim, false)
    }

    private fun toggle(visible: Boolean, animate: Boolean, force: Boolean) {
        if (mVisible != visible || force) {
            mVisible = visible
            if (height == 0 && !force && viewTreeObserver.isAlive) {
                viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
                    override fun onPreDraw(): Boolean {
                        if (viewTreeObserver.isAlive) {
                            viewTreeObserver.removeOnPreDrawListener(this)
                        }
                        toggle(visible, animate, true)
                        return true
                    }

                })

                return
            }

            val translationY = (if (visible) 0 else height).toFloat()
            if (animate)
                animate().setInterpolator(mInterpolator).setDuration(TRANSLATE_DURATION_MILLIS).translationY(translationY)
            else
                ViewCompat.setTranslationY(this, translationY)
        }
    }

    interface OnTabSelectedListener {
        fun onTabSelected(position: Int, prePostion: Int)
        fun onTabUnselected(position: Int)
        fun onTabReselected(position: Int)
    }

    class SavedState : BaseSavedState {
        var postion: Int = 0

        constructor(source: Parcel) : super(source) {
            postion = source.readInt()
        }

        constructor(source: Parcelable, position: Int) : super(source) {
            this.postion = position
        }

        override fun writeToParcel(out: Parcel?, flags: Int) {
            super.writeToParcel(out, flags)
            out?.writeInt(postion)
        }

        companion object {
            @JvmField
            val CREATOR = object : Parcelable.Creator<SavedState> {

                override fun createFromParcel(source: Parcel) = SavedState(source)

                override fun newArray(size: Int) = arrayOfNulls<SavedState>(size)

            }
        }
    }
}